package com.google.code.sysarch.v4.main;

import java.awt.Dimension;
import com.google.code.sysarch.v4.graphical.*;
import com.google.code.sysarch.v4.network.*;

import java.net.*;

@SuppressWarnings("serial")
public class OptionFrame extends javax.swing.JFrame {

    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JCheckBox jCheckBox6;
    private javax.swing.JCheckBox jCheckBox7;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JRadioButton jRadioButton7;
    private javax.swing.JRadioButton jRadioButton9;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    
    

    public OptionFrame() {
        this.setPreferredSize(new Dimension(430, 530));
        this.setTitle("Schiffe versenken");
        initComponents();
        
        InetAddress inet;
	
        try {
            inet = InetAddress.getLocalHost();
            jLabel12.setText(inet.getHostName());
        } catch (UnknownHostException e) {}  
        this.setLocationRelativeTo(null);
    }
    

    // <editor-fold defaultstate="collapsed" desc=" Erzeugter Quelltext ">                          
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jRadioButton9 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jTextField8 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTabbedPane = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jCheckBox2 = new javax.swing.JCheckBox();
        jRadioButton1 = new javax.swing.JRadioButton();
        jLabel13 = new javax.swing.JLabel();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jCheckBox5 = new javax.swing.JCheckBox();
        jLabel14 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jCheckBox6 = new javax.swing.JCheckBox();
        jCheckBox7 = new javax.swing.JCheckBox();
        jSlider1 = new javax.swing.JSlider();
        jLabel15 = new javax.swing.JLabel();
        jRadioButton7 = new javax.swing.JRadioButton();
        jTextField10 = new javax.swing.JTextField();

        getContentPane().setLayout(null);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jPanel3.setLayout(null);

        buttonGroup3.add(jRadioButton9);
        jRadioButton9.setText("Hanno-kompatibel");
        jRadioButton9.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton9.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioButton9.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                HannoMode(evt);
            }
        });

        jPanel3.add(jRadioButton9);
        jRadioButton9.setBounds(180, 140, 190, 18);

        buttonGroup3.add(jRadioButton6);
        jRadioButton6.setText("Liefl\u00e4nder-Modus");
        jRadioButton6.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton6.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioButton6.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                switchToLiefl(evt);
            }
        });

        jPanel3.add(jRadioButton6);
        jRadioButton6.setBounds(180, 170, 160, 18);

        buttonGroup3.add(jRadioButton5);
        jRadioButton5.setSelected(true);
        jRadioButton5.setText("Klassischer Modus");
        jRadioButton5.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton5.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jRadioButton5.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                ClassicMode(evt);
            }
        });

        jPanel3.add(jRadioButton5);
        jRadioButton5.setBounds(180, 110, 160, 18);

        jCheckBox1.setText("Schiffe d\u00fcrfen sich ber\u00fchren");
        jCheckBox1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox1.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel3.add(jCheckBox1);
        jCheckBox1.setBounds(20, 60, 200, 18);

        jTextField8.setText("5");
        jPanel3.add(jTextField8);
        jTextField8.setBounds(320, 220, 30, 22);

        jLabel7.setText("Schussdifferenz:");
        jPanel3.add(jLabel7);
        jLabel7.setBounds(210, 220, 110, 16);

        jTabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabChanged(evt);
            }
        });

        jPanel2.setLayout(null);

        jLabel1.setText("Verbinden zu:");
        jPanel2.add(jLabel1);
        jLabel1.setBounds(20, 20, 86, 16);

        jPanel2.add(jTextField9);
        jTextField9.setBounds(120, 20, 210, 22);

        jButton2.setText("Verbindung herstellen");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectToGame(evt);
            }
        });

        jPanel2.add(jButton2);
        jButton2.setBounds(80, 60, 200, 29);

        jTabbedPane.addTab("Client-Modus", jPanel2);

        jPanel1.setLayout(null);

        jButton1.setText("Spiel er\u00f6ffnen");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startGame(evt);
            }
        });

        jPanel1.add(jButton1);
        jButton1.setBounds(95, 60, 160, 29);

        jLabel11.setText("Aktueller Hostname:");
        jPanel1.add(jLabel11);
        jLabel11.setBounds(10, 10, 130, 16);

        jLabel12.setText("unbekannt");
        jPanel1.add(jLabel12);
        jLabel12.setBounds(10, 30, 330, 16);

        jTabbedPane.addTab("Server-Modus", jPanel1);

        jPanel3.add(jTabbedPane);
        jTabbedPane.setBounds(10, 290, 370, 150);

        jTextField1.setText("1");
        jPanel3.add(jTextField1);
        jTextField1.setBounds(100, 110, 30, 22);

        jLabel4.setText("Anzahl 3er:");
        jPanel3.add(jLabel4);
        jLabel4.setBounds(20, 170, 71, 16);

        jLabel3.setText("Anzahl 2er:");
        jPanel3.add(jLabel3);
        jLabel3.setBounds(20, 140, 71, 16);

        jLabel5.setText("Anzahl 4er:");
        jPanel3.add(jLabel5);
        jLabel5.setBounds(20, 200, 71, 16);

        jLabel8.setText("Spielfeldgr\u00f6\u00dfe:");
        jPanel3.add(jLabel8);
        jLabel8.setBounds(20, 20, 100, 16);

        jLabel9.setText("Anzahl 1er:");
        jPanel3.add(jLabel9);
        jLabel9.setBounds(20, 110, 71, 16);

        jTextField2.setText("3");
        jPanel3.add(jTextField2);
        jTextField2.setBounds(100, 140, 30, 22);

        jTextField3.setText("2");
        jPanel3.add(jTextField3);
        jTextField3.setBounds(100, 170, 30, 22);

        jTextField5.setText("1");
        jPanel3.add(jTextField5);
        jTextField5.setBounds(100, 230, 30, 22);

        jTextField4.setText("2");
        jPanel3.add(jTextField4);
        jTextField4.setBounds(100, 200, 30, 22);

        jTextField6.setText("10");
        jPanel3.add(jTextField6);
        jTextField6.setBounds(120, 20, 30, 22);

        jLabel2.setText("x");
        jPanel3.add(jLabel2);
        jLabel2.setBounds(160, 20, 10, 16);

        jTextField7.setText("10");
        jPanel3.add(jTextField7);
        jTextField7.setBounds(180, 20, 30, 22);

        jLabel10.setText("(Breite x H\u00f6he)");
        jPanel3.add(jLabel10);
        jLabel10.setBounds(220, 20, 100, 16);

        jLabel6.setText("Anzahl 5er:");
        jPanel3.add(jLabel6);
        jLabel6.setBounds(20, 230, 71, 16);

        jLabel16.setText("Maximale");
        jPanel3.add(jLabel16);
        jLabel16.setBounds(210, 200, 100, 16);

        jTabbedPane1.addTab("Spiel", jPanel3);

        jPanel4.setLayout(null);

        jCheckBox2.setSelected(true);
        jCheckBox2.setText("Musik an");
        jCheckBox2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox2.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel4.add(jCheckBox2);
        jCheckBox2.setBounds(230, 20, 130, 18);

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("800 x 600");
        jRadioButton1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton1.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel4.add(jRadioButton1);
        jRadioButton1.setBounds(230, 150, 110, 18);

        jLabel13.setText("Aufl\u00f6sung:");
        jPanel4.add(jLabel13);
        jLabel13.setBounds(230, 100, 80, 16);

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("1024 x 768");
        jRadioButton2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton2.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel4.add(jRadioButton2);
        jRadioButton2.setBounds(230, 170, 94, 18);

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("1280 x 1024");
        jRadioButton3.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton3.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel4.add(jRadioButton3);
        jRadioButton3.setBounds(230, 190, 102, 18);

        buttonGroup1.add(jRadioButton4);
        jRadioButton4.setText("1920 x 1200");
        jRadioButton4.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton4.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel4.add(jRadioButton4);
        jRadioButton4.setBounds(230, 210, 102, 18);

        jCheckBox3.setText("Vollbild-Modus");
        jCheckBox3.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox3.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel4.add(jCheckBox3);
        jCheckBox3.setBounds(20, 20, 170, 18);

        jCheckBox4.setSelected(true);
        jCheckBox4.setText("Soundeffekte an");
        jCheckBox4.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox4.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel4.add(jCheckBox4);
        jCheckBox4.setBounds(230, 50, 130, 18);

        jCheckBox5.setSelected(true);
        jCheckBox5.setText("Eigenes Pixelformat");
        jCheckBox5.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox5.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jCheckBox5.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                PixelFormatChanged(evt);
            }
        });

        jPanel4.add(jCheckBox5);
        jCheckBox5.setBounds(20, 110, 170, 18);

        jLabel14.setText("Multisample:");
        jPanel4.add(jLabel14);
        jLabel14.setBounds(50, 140, 90, 16);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "4", "8" }));
        jPanel4.add(jComboBox1);
        jComboBox1.setBounds(50, 160, 100, 27);

        jCheckBox6.setSelected(true);
        jCheckBox6.setText("Reflexionen");
        jCheckBox6.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox6.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel4.add(jCheckBox6);
        jCheckBox6.setBounds(20, 50, 140, 18);

        jCheckBox7.setSelected(true);
        jCheckBox7.setText("Bojen zeichnen");
        jCheckBox7.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox7.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel4.add(jCheckBox7);
        jCheckBox7.setBounds(20, 80, 140, 18);

        jSlider1.setMaximum(10);
        jSlider1.setValue(4);
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                fishSlide(evt);
            }
        });

        jPanel4.add(jSlider1);
        jSlider1.setBounds(30, 300, 350, 29);

        jLabel15.setText("Anzahl der Fische:");
        jPanel4.add(jLabel15);
        jLabel15.setBounds(20, 270, 120, 16);

        buttonGroup1.add(jRadioButton7);
        jRadioButton7.setText("640 x 480");
        jRadioButton7.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton7.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jPanel4.add(jRadioButton7);
        jRadioButton7.setBounds(230, 130, 86, 18);

        jTextField10.setText("4");
        jPanel4.add(jTextField10);
        jTextField10.setBounds(140, 270, 40, 22);

        jTabbedPane1.addTab("Optionen", jPanel4);

        getContentPane().add(jTabbedPane1);
        jTabbedPane1.setBounds(10, 10, 410, 490);

        pack();
    }// </editor-fold>                        

    
    private void tabChanged(javax.swing.event.ChangeEvent evt) {                            
        boolean enabled = false;        
        if (jTabbedPane.getSelectedIndex() == 1) {
            // Server-Mode
           enabled = true;
        } else {
            // Client-Mode
            enabled = false;
        }
        
        jTextField1.setEnabled(enabled);
        jTextField2.setEnabled(enabled);
        jTextField3.setEnabled(enabled);
        jTextField4.setEnabled(enabled);
        jTextField5.setEnabled(enabled);
        jTextField6.setEnabled(enabled);
        jTextField7.setEnabled(enabled);
        jTextField8.setEnabled(enabled);
        jCheckBox1.setEnabled(enabled);
        jRadioButton5.setEnabled(enabled); 
        jRadioButton6.setEnabled(enabled);
        jRadioButton9.setEnabled(enabled);
        jLabel2.setEnabled(enabled);
        jLabel3.setEnabled(enabled);
        jLabel4.setEnabled(enabled);
        jLabel5.setEnabled(enabled);
        jLabel6.setEnabled(enabled);
        jLabel7.setEnabled(enabled);
        jLabel8.setEnabled(enabled);
        jLabel9.setEnabled(enabled);
        jLabel10.setEnabled(enabled);
        jLabel16.setEnabled(enabled);
    }             
            
            
            
            private void fishSlide(javax.swing.event.ChangeEvent evt) {                           
                jTextField10.setText(Integer.toString(jSlider1.getValue()));
            }   
            

    private void PixelFormatChanged(javax.swing.event.ChangeEvent evt) {                                    
        jComboBox1.setEnabled(jCheckBox5.isSelected());
    }

    private void startGame(java.awt.event.ActionEvent evt) {                           
        this.setVisible(false);
        BattleShips.loadingWnd.display();
        
        BattleShips.network = new NetworkThread();
        BattleShips.network.start();
    }                          


    private void ClassicMode(javax.swing.event.ChangeEvent evt) {    	
    }

    private void HannoMode(javax.swing.event.ChangeEvent evt) {

    }    
    
    private void switchToLiefl(javax.swing.event.ChangeEvent evt) {
    	if (jTabbedPane.getSelectedIndex() == 0) return;
        jTextField8.setEnabled(jRadioButton6.isSelected());
    } 
            
    
    private void connectToGame(java.awt.event.ActionEvent evt) {                           
        this.setVisible(false);
        BattleShips.loadingWnd.display();
        
        String host = jTextField9.getText();
        BattleShips.network = new NetworkThread(host);
        BattleShips.network.start();
        
        BattleShips.game = new Game("Schiffe versenken");
        new Thread(BattleShips.game).start();
    }
    
    
    public int getFieldWidth() {
    	return Integer.valueOf(jTextField6.getText());
    }


    public int getFieldHeight() {
    	return Integer.valueOf(jTextField7.getText());
    }
    
    
    public boolean shipsCanTouch() {
    	return jCheckBox1.isSelected();
    }
    
    
    public int ship1Count() {
    	return Integer.valueOf(jTextField1.getText());
    }
    
    
    public int ship2Count() {
    	return Integer.valueOf(jTextField2.getText());
    }
    
    
    public int ship3Count() {
    	return Integer.valueOf(jTextField3.getText());
    }
    
        
    public int ship4Count() {
    	return Integer.valueOf(jTextField4.getText());
    }
    
    
    public int ship5Count() {
    	return Integer.valueOf(jTextField5.getText());
    }
    
    
    public int maxCountDifference() {
    	return Integer.valueOf(jTextField8.getText());
    }

    public boolean isFullscreen() {
    	return jCheckBox3.isSelected();
    }
    
    public boolean isSoundEnabled() {
    	return jCheckBox4.isSelected();
    }
    
    public boolean useCustomPixelFormat() {
    	return jCheckBox5.isSelected();
    }
    
    public boolean isMusicEnabled() {
    	return jCheckBox2.isSelected();
    }
    
    public int getMultisample() {
    	return Integer.parseInt((String) jComboBox1.getModel().getElementAt(jComboBox1.getSelectedIndex()));
    }
    
    public int getResolutionWidth() {
    	if (jRadioButton1.isSelected()) {
    		return 800;
    	} else if (jRadioButton2.isSelected()) {
    		return 1024;
    	} else if (jRadioButton3.isSelected()) {
    		return 1280;
    	} else if (jRadioButton4.isSelected()) {
    		return 1920;
    	} else {
    		return 640;
    	}
    }
    
    public int getResolutionHeight() {
    	if (jRadioButton1.isSelected()) {
    		return 600;
    	} else if (jRadioButton2.isSelected()) {
    		return 768;
    	} else if (jRadioButton3.isSelected()) {
    		return 1024;
    	} else if (jRadioButton4.isSelected()) {
    		return 1200;
    	} else {
    		return 480;
    	}
    }
    
    
    
    public int getNumFish() {
    	return Integer.valueOf(jTextField10.getText());
    }

    public boolean isReflectionsEnabled() {
    	return jCheckBox6.isSelected();
    }

    public boolean isBuoyEnabled() {
    	return jCheckBox7.isSelected();
    }

    public int getGameType() {
       	if (jRadioButton5.isSelected()) return Game.GAMETYPE_CLASSIC; else
       	if (jRadioButton6.isSelected()) return Game.GAMETYPE_LIEFLAENDER; else
       	if (jRadioButton9.isSelected()) return Game.GAMETYPE_HANNO; else
       		return -1;
    }
    
}
